# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2022/7/21 0021 23:46
# @Author    :jiaxuesong


import unittest
from ddt import ddt,data,unpack
from hight_interface_test.common.readExcle import *
from hight_interface_test.common.configHttp import *
from hight_interface_test.common.writeExcle import *
from hight_interface_test.common.preSolve import *


#调用实例方法获取测试数据
re_excle = ReadExcle()
data_object = re_excle.read()
#创建用例，定义一个测试类
@ddt
class TestCase(unittest.TestCase):
#使用ddt装饰器，对每条用例进行调用
    @data(*data_object)
    @unpack
    def test_case(self, id, interfaceUrl, name, method, value, header, rely, caseid, expect, real, status):
        value = eval(value)
        expect = int(expect)
        header = eval(header)
        # 执行前置用例
        # 实例化
        ps = Presovlve(data_object)
        # 调用实例方法,拿到对应header的value
        header, value = ps.presolve(id)
    #创建测试用例方法
        # 实例化confighttp类，进行调用
        #实例化confighttp方法
        res = ConfigHttp(method, interfaceUrl, eval(value), eval(header))
        re = res.run()
        #转换返回格式
        re_dict = re.json()
        real = re_dict['errorCode']
        #对接口返回进行断言
        try:
            if real == expect:
                status = 'pass'
            else:
                status = 'fail'
            # 实例化写入方法
            wr = WriteExcle()
            wr.write(int(id), 9, real, status)
        except Exception as msg:
            print(f'接口调用报错了，报错信息是：{msg}')
            raise
        #对接口进行断言
        self.assertEqual(real, expect, '不通过！！！')

if __name__ == '__main__':
    unittest.main(verbosity=2)